import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'

import { createRouter, createWebHistory } from 'vue-router'

const MallLayout = () => import('@/mall-layout/layout.vue')
const Authentication = () => import('@/pages/authentication/index.vue')

export const constantRouterMap: AppRouteRecordRaw[] = [
    {
        children: [
            {
                component: () => import('@/pages/issure/index.vue'),
                meta: {
                    title: '反馈',
                },
                name: 'root-issure',
                path: '/issure',
            },
            {
                component: () => import('@/pages/issure/detail.vue'),
                meta: {
                    title: '反馈详情',
                },
                name: 'root-issure-detail',
                path: '/issure/detail',
            },
            {
                component: () => import('@/pages/tender/index.vue'),
                meta: {
                    title: '首页-home',
                },
                name: 'root-index',
                path: '/',
            },
            {
                component: () => import('@/pages/help-center/index.vue'),
                meta: {
                    title: '帮助中心',
                },
                name: 'help-center',
                path: 'help-center',
            },
            // {
            //     component: () => import('@/pages/home/index.vue'),
            //     meta: {
            //         title: '首页-更多',
            //     },
            //     name: 'root-home',
            //     path: '/home',
            // },

            {
                component: () => import('@/pages/non-tender/index.vue'),
                meta: {
                    title: '首页-非招标专区',
                },
                name: 'root-non-tender',
                path: '/non-tender',
            },
            {
                component: () => import('@/pages/apply/index.vue'),
                meta: {
                    title: '首页-报名',
                },
                name: 'root-apply',
                path: '/apply',
            },

            {
                children: [
                    {
                        component: () => import('@/pages/authentication/component/agreement.vue'),
                        meta: {
                            title: '首页-agreement',
                        },
                        name: 'authentication-agreement',
                        path: '/authentication/agreement',
                    },

                    {
                        component: () => import('@/pages/authentication/component/finish.vue'),
                        meta: {
                            title: '首页-finish',
                        },
                        name: 'authentication-finish',
                        path: '/authentication/finish',
                    },
                    {
                        component: () => import('@/pages/authentication/component/mode.vue'),
                        meta: {
                            title: '首页-mode',
                        },
                        name: 'authentication-mode',
                        path: '/authentication/mode',
                    },
                    {
                        component: () => import('@/pages/authentication/component/profile.vue'),
                        meta: {
                            title: '首页-profile',
                        },
                        name: 'authentication-profile',
                        path: '/authentication/profile',
                    },
                    {
                        component: () => import('@/pages/authentication/component/remit-item.vue'),
                        meta: {
                            title: '首页-remit-item',
                        },
                        name: 'authentication-remit-item',
                        path: '/authentication/remit-item',
                    },
                    {
                        component: () => import('@/pages/authentication/component/result.vue'),
                        meta: {
                            title: '首页-result',
                        },
                        name: 'authentication-result',
                        path: '/authentication/result',
                    },

                ],
                component: Authentication,
                meta: {
                    hidden: true,
                    noTagsView: true,
                    title: '企业认证',
                },
                name: 'authentication',
                path: '/authentication',
            },

        ],
        component: MallLayout,
        meta: {
            hidden: true,
            noTagsView: true,
            title: '首页',
        },
        name: 'root',
        path: '/',
    },

    {
        children: [
            {
                component: () => import('@fl/views/redirect/redirect.vue'),
                meta: {},
                name: 'Redirect',
                path: '/redirect/:path(.*)',
            },

            {
                component: () => import('@fl/views/error/404.vue'),
                meta: {
                    hidden: true,
                    noTagsView: true,
                    title: '404',
                },
                name: 'NoFind',
                path: '/404',
            },
        ],
        component: MallLayout,
        meta: {
            hidden: true,
            noTagsView: true,
        },
        name: 'Redirect',
        path: '/redirect',
    },
    // {
    //     children: [
    //         {
    //             component: () => import('@fl/origin-components/Personal/PersonalCenter.vue'),
    //             meta: {
    //                 hidden: true,
    //                 title: '个人中心',
    //             },
    //             name: 'PersonalCenter',
    //             path: 'personal-center',
    //         },
    //     ],
    //     component: MallLayout,
    //     meta: {
    //         hidden: true,
    //         title: '个人中心',
    //     },
    //     name: 'Personal',
    //     path: '/personal',
    //     redirect: '/personal/personal-center',
    // },
    {
        component: () => import('@/pages/login/index.vue'),
        meta: {
            hidden: true,
            noTagsView: true,
            title: '登录',
        },
        name: 'login',
        path: '/login',
    },
    {
        component: () => import('@/pages/token/index.vue'),
        meta: {
            hidden: true,
            noTagsView: true,
            title: '首页-授权',
        },
        name: 'token',
        path: '/setToken',
    },
    {
        component: () => import('@/pages/register/register.vue'),
        meta: {
            hidden: true,
            noTagsView: true,
            title: '注册',
        },
        name: 'register',
        path: '/register',
    },
    {
        component: () => import('@/pages/forget-password/forget-password.vue'),
        meta: {
            hidden: true,
            noTagsView: true,
            title: '忘记密码',
        },
        name: 'forget-password',
        path: '/forget-password',
    },

]

export const asyncRouterMap: AppRouteRecordRaw[] = []
const router = createRouter({
    history: createWebHistory(),
    routes: constantRouterMap as RouteRecordRaw[],
    scrollBehavior: () => ({ left: 0, top: 0 }),
    strict: true,
})

export function resetRouter(): void {
    const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
    router.getRoutes().forEach((route) => {
        const { name } = route
        if (name && !resetWhiteNameList.includes(name as string))
            router.hasRoute(name) && router.removeRoute(name)
    })
}

export function setupRouter(app: App<Element>) {
    app.use(router)
}

export default router
